namespace TimeSheet.Protected.View.Reports
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;
	using TimeSheetBLL;

	/// <summary>
	///		Summary description for MainControl.
	/// </summary>
	public abstract class MainControl : System.Web.UI.UserControl
	{
		protected System.Web.UI.WebControls.DropDownList ddlClients;
		protected System.Web.UI.WebControls.DropDownList ddlStaff;
		protected eWorld.UI.CalendarPopup cpProjectTo;
		protected System.Web.UI.WebControls.CheckBox cbShowNonBillable;
		protected System.Web.UI.WebControls.Button btnProjectReport;
		protected System.Web.UI.WebControls.Button btnStaffReport;
		protected System.Web.UI.WebControls.Label lblProjectMessage;
		protected System.Web.UI.WebControls.Label lblStaffMessage;
		protected System.Web.UI.WebControls.CheckBox cbShowAll;
		protected System.Web.UI.WebControls.CheckBox cbOvertime;
		protected System.Web.UI.WebControls.Label lblSummaryMessage;
		protected System.Web.UI.WebControls.Button btnGenerateSummary;
		protected eWorld.UI.CalendarPopup cpProjectFrom;
		protected eWorld.UI.CalendarPopup cpStaffTo;
		protected eWorld.UI.CalendarPopup cpStaffFrom;
		protected eWorld.UI.CalendarPopup cpSummaryEnd;
		protected eWorld.UI.CalendarPopup cpSummaryStart;
		protected eWorld.UI.CalendarPopup cpActivityStart;
		protected eWorld.UI.CalendarPopup cpActivityEnd;
		protected System.Web.UI.WebControls.Button btnActivity;
		protected System.Web.UI.WebControls.DropDownList ddlActivities;
		protected eWorld.UI.CalendarPopup cpTotalsFrom;
		protected eWorld.UI.CalendarPopup cpTotalsTo;
		protected System.Web.UI.WebControls.Button btnTotals;
		protected System.Web.UI.WebControls.DropDownList ddlProjects;

		private void Page_Load(object sender, System.EventArgs e)
		{
			if( !IsPostBack )
			{
				BindClientsAndProjects();
				BindStaff();
				BindActivities();
				this.cpProjectFrom.SelectedDate = Utils.GetMonthStart( DateTime.Now.AddMonths( -1 ) );
				this.cpProjectTo.SelectedDate = Utils.GetMonthEnd( DateTime.Now.AddMonths( -1 ) );
				this.cpStaffFrom.SelectedDate = this.cpProjectFrom.SelectedDate;
				this.cpStaffTo.SelectedDate = this.cpProjectTo.SelectedDate;
				this.cpSummaryEnd.SelectedDate = this.cpProjectTo.SelectedDate;
				this.cpSummaryStart.SelectedDate = this.cpProjectFrom.SelectedDate;
				this.cpActivityEnd.SelectedDate = this.cpProjectTo.SelectedDate;
				this.cpActivityStart.SelectedDate = this.cpProjectFrom.SelectedDate;
				this.cpTotalsTo.SelectedDate = this.cpProjectTo.SelectedDate;
				this.cpTotalsFrom.SelectedDate = this.cpProjectFrom.SelectedDate;
			}
		}

		private void BindActivities()
		{
			this.ddlActivities.DataSource = BrokerFactory.SelectAll( new Activity() );
			this.ddlActivities.DataTextField = "activity_name";
			this.ddlActivities.DataValueField = "activity_id";
			this.ddlActivities.DataBind();
		}

		private void BindStaff()
		{
			this.ddlStaff.DataSource = BrokerFactory.SelectAll( new User() );
			this.ddlStaff.DataTextField = "full_name";
			this.ddlStaff.DataValueField = "user_id";
			this.ddlStaff.DataBind();
		}

		private void BindClientsAndProjects()
		{
			this.ddlClients.DataSource = BrokerFactory.SelectAll( new Client() );
			this.ddlClients.DataTextField = "client_name";
			this.ddlClients.DataValueField = "client_id";
			this.ddlClients.DataBind();

			if( this.ddlClients.Items.Count > 0 )
			{
				this.BindProjects( Convert.ToInt64( this.ddlClients.SelectedItem.Value ) );
			}
		}

		private void BindProjects( long clientID )
		{
			Client client = new Client();
			client.ID = clientID;
			this.ddlProjects.DataSource = client.GetProjects();
			this.ddlProjects.DataTextField = "project_name";
			this.ddlProjects.DataValueField = "project_id";
			this.ddlProjects.DataBind();
		}

		private string GetStaffID()
		{
			return Server.UrlEncode( this.ddlStaff.SelectedItem.Value );
		}

		private string GetProjectID()
		{
			return Server.UrlEncode( this.ddlProjects.SelectedItem.Value );
		}

		private string GetActivityID()
		{
			return Server.UrlEncode( this.ddlActivities.SelectedItem.Value );
		}

		private string GetShowNonBillable()
		{
			return Server.UrlEncode( this.cbShowNonBillable.Checked.ToString() );
		}

		private string GetShowAll()
		{
			return Server.UrlEncode( this.cbShowAll.Checked.ToString() );
		}

		private string GetStaffFrom()
		{
			return Server.UrlEncode( this.cpStaffFrom.SelectedDate.ToString() );
		}

		private string GetStaffTo()
		{
			return Server.UrlEncode( this.cpStaffTo.SelectedDate.ToString() );
		}

		private string GetProjectFrom()
		{
			return Server.UrlEncode( this.cpProjectFrom.SelectedDate.ToString() );
		}

		private string GetProjectTo()
		{
			return Server.UrlEncode( this.cpProjectTo.SelectedDate.ToString() );
		}

		private string GetSummaryFrom()
		{
			return Server.UrlEncode( this.cpSummaryStart.SelectedDate.ToString() );
		}

		private string GetSummaryTo()
		{
			return Server.UrlEncode( this.cpSummaryEnd.SelectedDate.ToString() );
		}

		private string GetTotalsFrom()
		{
			return Server.UrlEncode( this.cpTotalsFrom.SelectedDate.ToString() );
		}

		private string GetTotalsTo()
		{
			return Server.UrlEncode( this.cpTotalsTo.SelectedDate.ToString() );
		}

		private string GetActivityFrom()
		{
			return Server.UrlEncode( this.cpActivityStart.SelectedDate.ToString() );
		}

		private string GetActivityTo()
		{
			return Server.UrlEncode( this.cpActivityEnd.SelectedDate.ToString() );
		}

		private string GetShowOvertime()
		{
			return Server.UrlEncode( this.cbOvertime.Checked.ToString() );
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.ddlClients.SelectedIndexChanged += new System.EventHandler(this.ddlClients_SelectedIndexChanged);
			this.btnProjectReport.Click += new System.EventHandler(this.btnProjectReport_Click);
			this.btnActivity.Click += new System.EventHandler(this.btnActivity_Click);
			this.btnStaffReport.Click += new System.EventHandler(this.btnStaffReport_Click);
			this.btnGenerateSummary.Click += new System.EventHandler(this.btnGenerateSummary_Click);
			this.btnTotals.Click += new System.EventHandler(this.btnTotals_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void ddlClients_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			try
			{
				BindProjects( Convert.ToInt64( this.ddlClients.SelectedItem.Value ) );
			}
			catch{}
		}

		private void btnStaffReport_Click(object sender, System.EventArgs e)
		{
			Response.Redirect( "StaffReport.aspx?id=" + this.GetStaffID() + 
				"&nonbillable=" + this.GetShowNonBillable() +
				"&from=" + this.GetStaffFrom() +
				"&to=" + this.GetStaffTo() +
				"&overtime=" + this.GetShowOvertime() );
		}

		private void btnProjectReport_Click(object sender, System.EventArgs e)
		{
			if( this.ddlProjects.Items.Count > 0 )
			{
				Response.Redirect( "ProjectReport.aspx?id=" + this.GetProjectID() +
					"&from=" + this.GetProjectFrom() + 
					"&to=" + this.GetProjectTo() +
					"&showall=" + this.GetShowAll() );
			}
			else
			{
				this.lblProjectMessage.Text = "No project has been selected.";
			}
		}

		private void btnGenerateSummary_Click(object sender, System.EventArgs e)
		{
				Response.Redirect( "ProjectSummaryReport.aspx?from=" + this.GetSummaryFrom() + 
					"&to=" + this.GetSummaryTo() );
		}

		private void btnActivity_Click(object sender, System.EventArgs e)
		{
			if( this.ddlActivities.Items.Count > 0 )
			{
				Response.Redirect( "ActivityReport.aspx?id=" + this.GetActivityID() + 
					"&from=" + this.GetActivityFrom() +
					"&to=" +this.GetActivityTo() );
			}
		}

		private void btnTotals_Click(object sender, System.EventArgs e)
		{
			Response.Redirect( "TotalsReport.aspx?from=" + this.GetTotalsFrom() +
				"&to=" + this.GetTotalsTo() );
		}

	}
}
